package com.lwj.utils;

import org.apache.poi.ss.formula.functions.T;
import org.springframework.util.StringUtils;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class CollectionTool {
    /**
     * 构建树结构
     * @param data
     * @return
     * @param <T>
     */
    @Deprecated
    public  static <T> List<T> buildTree(List<T> data){
        List<T> result=new ArrayList<>(data);
        data.stream().forEach(a->{
            data.stream().forEach(b->{
                Object menuId = CustomReflectUtil.invokeMethod(a, "id");
                Object parentId = CustomReflectUtil.invokeMethod(b, "parentId");
                if(menuId.equals(parentId)){
                    CustomReflectUtil.invokeMethod(a, "addChild",b);
                    result.remove(b);
                }
            });
        });
        return result;
    }

    /**
     * 构建树结构
     * @param data
     * @return
     * @param <T>
     */
    public  static <T extends BuildTreeBean> List<T> buildTree2(List<T> data){
        List<T> result=new ArrayList<>(data);
        data.stream().forEach(a->{
            data.stream().forEach(b->{
                String id = a.id();
                String parentId = b.parentId();
                if(Objects.equals(id,parentId)){
                    List children = a.children();
                    children.add(b);
                    result.remove(b);
                }
            });
        });
        return result;
    }
    /**
     * 逗号分割字符串转List
     * @return
     */
    public static List<String> strToList(String str){
        if(StringUtils.hasText(str)){
            return new ArrayList<>(Arrays.asList(str.split(",")));
        }else {
            return new ArrayList<>();
        }
    }
    /**
     * 可变参数转List
     * @param arr
     * @return
     */
    public static <T>  List<T> toList(T ... arr){
        return new ArrayList<>(Arrays.asList(arr));
    }
    /**
     * 数组转List
     * @param arr
     * @return
     */
    public static <T>  List<T> arrToList(T [] arr){
         return new ArrayList<>(Arrays.asList(arr));
    }

    /**
     * 根据parentTreeId获取所有子级
     * @param id
     * @param allData
     * @return
     * @param <T>
     */
    public static  <T extends BuildTreeBean>  List<T> getAllChildrenWithParentId(String id,List<T> allData) {
        List<T> children =new ArrayList<>();
        selectChildren(children, allData, id);
        return children;
    }
    private static <T extends BuildTreeBean> void selectChildren(List<T> children, List<T> allData, Object parentTreeId) {
        for (T data: allData) {
            String id = data.id();
            String parentId = data.parentId();
            if (parentId!= null && parentId.equals(parentTreeId)) {
                selectChildren(children, allData, id);
                children.add(data);
            }
        }
    }

    /**
     * 数组复制
     * @param resource 源数组
     * @param resourceStart 开始复制下标
     * @param target   目标数组
     * @param targetStart 开始复制的下标
     * @param num  复制的数量，要注意被复制的元素是否够，，和目标数组的大小
     */
    public static void arraycopy(Object resource,  int  resourceStart, Object target, int targetStart, int num){
         System.arraycopy(resource,resourceStart,target,targetStart,num);
    }

    /**
     * 集合转数组
     * @param list
     * @return
     */
    public static T [] listToArray(List<T> list){
       return list.toArray(new T[list.size()]);
    }

    /**
     * 集合切割
     *
     * @param list
     * @param pageSize
     * @param <T>
     * @return
     */
    public static <T> List<List<T>> splitList(List<T> list, int pageSize) {
        List<List<T>> listArray = new ArrayList<List<T>>();
        for (int i = 0; i < list.size(); i += pageSize) {
            int toIndex = i + pageSize > list.size() ? list.size() : i + pageSize;
            listArray.add(list.subList(i, toIndex));
        }
        return listArray;
    }
}
